/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package restaurante.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import restaurante.bancodados.CriaConexao;
import restaurante.logica.Funcionario;

/**
 *
 * @author Fernanda
 */
public class FuncionarioDao {
    private Connection conexao;

    public FuncionarioDao() throws SQLException{
        this.conexao = CriaConexao.getConexao();
    }

    public void adiciona(Funcionario f1) throws SQLException{
        String  sql = "insert into funcionario (nome, endereco, telefone, salario, data_admissao, funcao)" +
                "values (?,?,?,?,?,?)";
        PreparedStatement stmt = conexao.prepareStatement(sql);

        // Seta os valores
        stmt.setString(1, f1.getNome());
        stmt.setString(2, f1.getEndereco());
        stmt.setString(3, f1.getTelefone());
        stmt.setString(4, String.valueOf(f1.getSalario()));
        stmt.setString(5, f1.getData_adminissao());
        stmt.setString(6, f1.getFuncao());

        // Executa o código SQL
        stmt.execute();
        stmt.close();
    }

    public  List<Funcionario> getLista1() throws SQLException{
        String sql = "Select * from funcionario";
        PreparedStatement stmt = this.conexao.prepareStatement(sql);
        ResultSet rs = stmt.executeQuery();

        List<Funcionario> minhaLista = new ArrayList<Funcionario>();

        while(rs.next()){
            Funcionario f1 = new Funcionario();
            f1.setID(rs.getInt("id"));
            f1.setNome(rs.getString("nome"));
            f1.setEndereco(rs.getString("endereco"));
            f1.setTelefone(rs.getString("telefone"));
            f1.setSalario(Float.parseFloat(rs.getString("salario")));
            f1.setData_adminissao(rs.getString("data_admissao"));
            f1.setFuncao(rs.getString("funcao"));

            minhaLista.add(f1);
        }

        rs.close();
        stmt.close();
        return minhaLista;
    }

        public  List<Funcionario> getLista(String busca) throws SQLException{
        String sql = "Select * from funcionario where nome like ?";
        PreparedStatement stmt = this.conexao.prepareStatement(sql);

        stmt.setString(1, busca);       // inserção do caracter de busca.

        ResultSet rs = stmt.executeQuery();

        List<Funcionario> minhaLista = new ArrayList<Funcionario>();

        while(rs.next()){
            Funcionario f1 = new Funcionario();
            f1.setID(rs.getInt("id"));
            f1.setNome(rs.getString("nome"));
            f1.setEndereco(rs.getString("endereco"));
            f1.setTelefone(rs.getString("telefone"));
            f1.setSalario(Float.parseFloat(rs.getString("salario")));
            f1.setData_adminissao(rs.getString("data_admissao"));
            f1.setFuncao(rs.getString("funcao"));

            minhaLista.add(f1);
        }

        rs.close();
        stmt.close();
        return minhaLista;
    }


    public void altera(Funcionario f1) throws SQLException{
        String sql = "update funcionario set nome=?, endereco=?" +
                        ", telefone=?, salario=?, data_admissao=?, funcao=?" +
                        "where id=?";
        PreparedStatement stmt = conexao.prepareStatement(sql);

        // Seta os valores

        stmt.setString(1, f1.getNome());
        stmt.setString(2, f1.getEndereco());
        stmt.setString(3, f1.getTelefone());
        stmt.setString(4, String.valueOf(f1.getSalario()));
        stmt.setString(5, f1.getData_adminissao());
        stmt.setString(6, f1.getFuncao());
        stmt.setString(7, String.valueOf(f1.getID()));

        // Executa o código SQL
        stmt.execute();
        stmt.close();

    }

    public void remove(Funcionario f1) throws SQLException{
        String sql = "delete from funcionario where id=?";
        PreparedStatement stmt = conexao.prepareStatement(sql);

        stmt.setString(1, String.valueOf(f1.getID()));

        stmt.execute();
        stmt.close();

    }


}
